import {allPoints} from "@/api/operate/project-management/projectManagement/architectural-settings";
import { allfacilitPoints } from '@/api/operate/project-management/projectManagement/architectural-settings/index';
import { allPointsDevice as alldevicePoints } from '@/api/operate/project-management/projectManagement/architectural-settings/index';
import { allPointsVideo } from '@/api/operate/project-management/projectManagement/architectural-settings/index';
import { allPoints as allDevicePoints } from '@/api/operate/project-management/projectManagement/architectural-settings/index';
import {isArray, isObject} from "@pureadmin/utils";
import { log } from "console";

interface changeMarker {
  lng: any,
  lat: any
}

type pointType = "device" | 'video' |'building' | 'facilit'

const apiMap = {
  "device": alldevicePoints,
  'video': allPointsVideo,
  'building': allPoints,
  'facilit': allfacilitPoints,
}


export const usePoints = (pointType:pointType) => {
  const api = apiMap[pointType]
  //获取地图已标记点位
  // 编辑中的 center  [lng,lat]
  const getMapPoints = async (projectId, excludeEdit: boolean, center?: Array<any>) => {
    return new Promise((resolve, reject) => {
      api({projectId:projectId}).then(res => {
        let markers = res.result.map(item => ({ lng: item.markPoint.lng, lat: item.markPoint.lat }));
        //排除添加的点位
        if(excludeEdit) {
          markers = markers.filter(item => item.lng !== center[0] && item.lat !== center[1])
        }
        resolve(markers)
      })
    })
  }


  //获取摄像点位
  //excludeEdit 是否排除已添加点位
  const getMarkerPoints = async (excludeEdit: boolean, changeMarker?: Array<changeMarker> | Object,parms?:any) => {
   
    return new Promise((resolve, reject) => {
      //获取已经标记过的点位
      api(parms).then(res => {
        let markers = res.result.map(item => Object.assign({}, item.drawingPoint, item));
       
        //排除添加的点位
        if (excludeEdit) {
          if (isArray(changeMarker) && changeMarker.length > 0) {
            markers = markers.filter(item => item.lng !== changeMarker[0].lng && item.lat !== changeMarker[0].lat)
          }else if(isObject(changeMarker)) {
            markers = markers.filter(item => item.lng !== changeMarker.lng && item.lat !== changeMarker.lat)
          }
        }
        markers = markers.filter(item => item.lng  && item.lat )
        // console.log("获取标记已知点1",markers);
        resolve(markers)
      })
    })
  }

  return {
    getMarkerPoints,
    getMapPoints
  }
}
